<?php
namespace App\Http\Controllers\Laifei\Shop;

use App\Http\Controllers\Controller;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ShopStoreController extends Controller
{

    public function index()
    {
        $all = $this->all;
        $pageSize = $all['pageSize'] ?? 20;
        $pageNo = $all['pageNo'] ?? 1;

        try{

            $where = function ($query) use ($all) {
                if(isset($all['shop_id'])){
                    $query->where('shop.shop_id','=',$all['shop_id']);
                }
                if(isset($all['shop_name'])){
                    $query->where('shop.shop_name','like','%'.$all['shop_name'].'%');
                }
                if(isset($all['contact_man'])){
                    $query->where('shop.contact_man','like','%'.$all['contact_man'].'%');
                }
                if(isset($all['contact_phone'])){
                    $query->where('shop.contact_phone','=',$all['contact_phone']);
                }
                if(isset($all['openid'])){
                    $query->where('lu.openid','=',$all['openid']);
                }
                if(isset($all['nickname'])){
                    $query->where('lu.nickname','like','%'.$all['nickname'].'%');
                }
                if(isset($all['phone'])){
                    $query->where('lu.phone','=',$all['phone']);
                }
                if(isset($all['verified'])){
                    $query->where('shop.verified', '=', $all['verified']);
                }
            };

            $total = DB::table('shop')
                ->leftJoin('lottery_users as lu','shop.we_id','=','lu.we_id')
                ->where($where)
                ->count();

            $info = DB::table('shop')
                ->leftJoin('lottery_users as lu','shop.we_id','=','lu.we_id')
                ->leftJoin('explore_order as eo','shop.shop_id','=','eo.shop_id')
                ->leftJoin('dict_area as da','shop.district','=','da.id')
                ->where($where)
                ->groupBy('shop_id')
                ->select('shop.shop_id','shop.we_id','shop.key_word','shop.shop_type','shop.shop_name','shop.contact_man','shop.contact_phone','shop.province','shop.city','shop.district','shop.address','shop.platform','shop.created_at',
                    'lu.openid','lu.nickname','lu.phone','da.short_name')
                ->selectRaw('COUNT(eo.eo_id) AS orders')
                ->orderByDesc('shop.shop_id')
                ->skip(($pageNo - 1) * $pageSize)
                ->take($pageSize)
                ->get()
                ->map(function($item){
                    $item->shop_type_name = Data::$laifeiShopTypes[$item->shop_type];
                    return $item;
                })
                ->toArray();

            return $this->setReturnJson(0,'success',['total'=>$total,'info'=>$info]);

        }catch (\Exception $e){
            Log::error(ShopStoreController::class,['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1,'查询失败');
        }

    }

}
